package com.sanhuo.security.filter;

import com.sanhuo.lib.ExceptionUtil;
import com.sanhuo.security.global.ResponseEnum;
import com.sanhuo.utils.basic.BeanUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 只拦截首页的访问
 */
@Component
public class HomeFilter extends HandlerInterceptorAdapter {

    private final String TOKENSTR = "token";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = obtainToken(request);
        ExceptionUtil.throwException(BeanUtil.isNull(token), ResponseEnum.INDEX_VERIFY_FAILE.getCode(), ResponseEnum.INDEX_VERIFY_FAILE.getMsg());
        return true;
    }


    public String obtainToken(HttpServletRequest request) {
        return request.getParameter(TOKENSTR);
    }
}
